package com.shanzmoo.admin.web.product;


import com.shanzmoo.base.bean.PageRespBase;
import com.shanzmoo.base.bean.SHttpResponse;
import com.shanzmoo.base.bean.SResponse;
import com.shanzmoo.core.cache.CurrentUserCache;
import com.shanzmoo.base.util.ObjectUtils;
import com.shanzmoo.db.product.entity.ProductAttributeEntity;
import com.shanzmoo.db.product.entity.vo.AttributeQueryVO;
import com.shanzmoo.db.product.entity.vo.ProductAttributeEntityVO;
import com.shanzmoo.db.product.service.IProductAttributeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * <p>
 * 商品属性 前端控制器
 * </p>
 *
 * @author Xueqz
 * @since 2020-12-07
 */
@Api(tags = "商品-商品规格")
@RestController
@RequestMapping("/api/product/attribute")
public class ProductAttributeController {

    @Autowired
    private IProductAttributeService productAttributeService;

    @ApiOperation(value = "商品规格list")
    @GetMapping("/attributeList")
    public SHttpResponse<PageRespBase<ProductAttributeEntity>> attributeList(AttributeQueryVO AttributeQueryVO) {
        SResponse<PageRespBase<ProductAttributeEntity>> re = productAttributeService.getAttributeList(AttributeQueryVO);
        return SHttpResponse.transform(re);
    }

    @ApiOperation(value = "添加商品规格")
    @GetMapping("/addAttribute")
    public SHttpResponse<Boolean> addAttribute(ProductAttributeEntity productAttributeEntity) {

        productAttributeEntity.setCreateUser(CurrentUserCache.getUserId());
        productAttributeService.save(productAttributeEntity);
        return SHttpResponse.ok(Boolean.TRUE);
    }

    @ApiOperation(value = "删除商品规格")
    @GetMapping("/delAttributeById")
    public SHttpResponse<Boolean> delAttributeById(@RequestParam(value = "attributeId") Integer attributeId) {

        ProductAttributeEntity productAttributeEntity = new ProductAttributeEntity();
        productAttributeEntity.setId(attributeId);
        productAttributeEntity.setUpdateUser(CurrentUserCache.getUserId());
        productAttributeEntity.setUpdateTime(LocalDateTime.now());
        productAttributeEntity.setDeleted(true);

        productAttributeService.updateById(productAttributeEntity);
        return SHttpResponse.ok(Boolean.TRUE);
    }

    @ApiOperation(value = "更新商品规格")
    @GetMapping("/updateAttribute")
    public SHttpResponse<Boolean> updateAttribute(ProductAttributeEntityVO productAttributeEntityVO) {
        ProductAttributeEntity productAttributeEntity = ObjectUtils.convert(productAttributeEntityVO, ProductAttributeEntity.class);
        productAttributeEntity.setUpdateUser(CurrentUserCache.getUserId());

        productAttributeService.updateById(productAttributeEntity);
        return SHttpResponse.ok(Boolean.TRUE);
    }

}
